package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.context.BaseContext;
import org.example.pojo.*;
import org.example.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/posts")
@Slf4j
public class PostController {

    @Autowired
    private PostService postService;

    @GetMapping
    public Result<PageResult<PostVO>> getPosts(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "latest") String type) {

        return Result.success(postService.getPosts(page, pageSize, type));
    }

    @PostMapping("/{postId}/like")
    public Result<Map<String, Object>> toggleLike(@PathVariable Long postId) {
        Long userId = BaseContext.getCurrentId();
        return Result.success(postService.toggleLike(userId, postId));
    }

    @PostMapping("/{postId}/comments")
    public Result<CommentVO> addComment(
            @PathVariable Long postId,
            @RequestBody Map<String, String> params) {
        Long userId = BaseContext.getCurrentId();
        return Result.success(postService.addComment(userId, postId, params.get("content")));
    }
} 